

package it.unibo.iss.domotica;


import java.util.ArrayList;
import java.util.Properties;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import it.unibo.iss.domotica.edDomain.*;

public class DomoticDomain {
	
	private ArrayList<IElettrodomestico> listEd = new ArrayList<IElettrodomestico>();
	private ArrayList<ISensor> sensors = new ArrayList<ISensor>();
	private String propertiesPath;
	private Properties prop = null;
	
	public DomoticDomain() {
		
		propertiesPath = "G:/Documenti/Universita/Magistrale/Ingegneria dei Sistemi Software M/workspace/domotica.scontrol/src/system.properties";
		
	}
	
	public void setListEd(ArrayList<IElettrodomestico> listEd) {
		this.listEd = listEd;
	}
	
	public ArrayList<IElettrodomestico> getListEd() {
		return listEd;
	}
	
	public void setSensors(ArrayList<ISensor> sensors) {
		this.sensors = sensors;
	}
	
	public ArrayList<ISensor> getSensors() {
		return sensors;
	}
	
	public void setPropertiesPath(String path) {
		propertiesPath = path;
		prop = null;
	}
	
	public ArrayList<IElettrodomestico> createAppliances() {
		
		
		listEd.add(new Elettrodomestico("edIB0", CatUsage.ESSENTIAL , CatConsumptionEnum.LOW));
		
		listEd.add(new Elettrodomestico("edIB1", CatUsage.ESSENTIAL , CatConsumptionEnum.LOW));
		
		listEd.add(new Elettrodomestico("edIB2", CatUsage.ESSENTIAL , CatConsumptionEnum.LOW));
		
		listEd.add(new Elettrodomestico("edSB0", CatUsage.OPTIONAL , CatConsumptionEnum.LOW));
		
		listEd.add(new Elettrodomestico("edSB1", CatUsage.OPTIONAL , CatConsumptionEnum.LOW));
		
		listEd.add(new Elettrodomestico("edSB2", CatUsage.OPTIONAL , CatConsumptionEnum.LOW));
		
		listEd.add(new Elettrodomestico("edIM0", CatUsage.ESSENTIAL , CatConsumptionEnum.MEDIUM));
		
		listEd.add(new Elettrodomestico("edIM1", CatUsage.ESSENTIAL , CatConsumptionEnum.MEDIUM));
		
		listEd.add(new Elettrodomestico("edIM2", CatUsage.ESSENTIAL , CatConsumptionEnum.MEDIUM));
		
		listEd.add(new Elettrodomestico("edSM0", CatUsage.OPTIONAL , CatConsumptionEnum.MEDIUM));
		
		listEd.add(new Elettrodomestico("edSM1", CatUsage.OPTIONAL , CatConsumptionEnum.MEDIUM));
		
		listEd.add(new Elettrodomestico("edSM2", CatUsage.OPTIONAL , CatConsumptionEnum.MEDIUM));
		
		listEd.add(new Elettrodomestico("edIA0", CatUsage.ESSENTIAL , CatConsumptionEnum.HIGH));
		
		listEd.add(new Elettrodomestico("edIA1", CatUsage.ESSENTIAL , CatConsumptionEnum.HIGH));
		
		listEd.add(new Elettrodomestico("edIA2", CatUsage.ESSENTIAL , CatConsumptionEnum.HIGH));
		
		listEd.add(new Elettrodomestico("edSA0", CatUsage.OPTIONAL , CatConsumptionEnum.HIGH));
		
		listEd.add(new Elettrodomestico("edSA1", CatUsage.OPTIONAL , CatConsumptionEnum.HIGH));
		
		listEd.add(new Elettrodomestico("edSA2", CatUsage.OPTIONAL , CatConsumptionEnum.HIGH));
		
		
		return listEd;
	}
	
	public ArrayList<ISensor> createSensors() throws Exception {
			
		
		
		sensors.add(new Sensor("edIB0", loadConsumFromProp(CatConsumptionEnum.LOW, 0), loadConsumFromProp(CatConsumptionEnum.LOW, 1)));
		
		
		
		sensors.add(new Sensor("edIB1", loadConsumFromProp(CatConsumptionEnum.LOW, 0), loadConsumFromProp(CatConsumptionEnum.LOW, 1)));
		
		
		
		sensors.add(new Sensor("edIB2", loadConsumFromProp(CatConsumptionEnum.LOW, 0), loadConsumFromProp(CatConsumptionEnum.LOW, 1)));
		
		
		
		sensors.add(new Sensor("edSB0", loadConsumFromProp(CatConsumptionEnum.LOW, 0), loadConsumFromProp(CatConsumptionEnum.LOW, 1)));
		
		
		
		sensors.add(new Sensor("edSB1", loadConsumFromProp(CatConsumptionEnum.LOW, 0), loadConsumFromProp(CatConsumptionEnum.LOW, 1)));
		
		
		
		sensors.add(new Sensor("edSB2", loadConsumFromProp(CatConsumptionEnum.LOW, 0), loadConsumFromProp(CatConsumptionEnum.LOW, 1)));
		
		
		
		sensors.add(new Sensor("edIM0", loadConsumFromProp(CatConsumptionEnum.MEDIUM, 0), loadConsumFromProp(CatConsumptionEnum.MEDIUM, 1)));
		
		
		
		sensors.add(new Sensor("edIM1", loadConsumFromProp(CatConsumptionEnum.MEDIUM, 0), loadConsumFromProp(CatConsumptionEnum.MEDIUM, 1)));
		
		
		
		sensors.add(new Sensor("edIM2", loadConsumFromProp(CatConsumptionEnum.MEDIUM, 0), loadConsumFromProp(CatConsumptionEnum.MEDIUM, 1)));
		
		
		
		sensors.add(new Sensor("edSM0", loadConsumFromProp(CatConsumptionEnum.MEDIUM, 0), loadConsumFromProp(CatConsumptionEnum.MEDIUM, 1)));
		
		
		
		sensors.add(new Sensor("edSM1", loadConsumFromProp(CatConsumptionEnum.MEDIUM, 0), loadConsumFromProp(CatConsumptionEnum.MEDIUM, 1)));
		
		
		
		sensors.add(new Sensor("edSM2", loadConsumFromProp(CatConsumptionEnum.MEDIUM, 0), loadConsumFromProp(CatConsumptionEnum.MEDIUM, 1)));
		
		
		
		sensors.add(new Sensor("edIA0", loadConsumFromProp(CatConsumptionEnum.HIGH, 0), loadConsumFromProp(CatConsumptionEnum.HIGH, 1)));
		
		
		
		sensors.add(new Sensor("edIA1", loadConsumFromProp(CatConsumptionEnum.HIGH, 0), loadConsumFromProp(CatConsumptionEnum.HIGH, 1)));
		
		
		
		sensors.add(new Sensor("edIA2", loadConsumFromProp(CatConsumptionEnum.HIGH, 0), loadConsumFromProp(CatConsumptionEnum.HIGH, 1)));
		
		
		
		sensors.add(new Sensor("edSA0", loadConsumFromProp(CatConsumptionEnum.HIGH, 0), loadConsumFromProp(CatConsumptionEnum.HIGH, 1)));
		
		
		
		sensors.add(new Sensor("edSA1", loadConsumFromProp(CatConsumptionEnum.HIGH, 0), loadConsumFromProp(CatConsumptionEnum.HIGH, 1)));
		
		
		
		sensors.add(new Sensor("edSA2", loadConsumFromProp(CatConsumptionEnum.HIGH, 0), loadConsumFromProp(CatConsumptionEnum.HIGH, 1)));
		
		
		
		return sensors;
	}
	
	private double loadConsumFromProp(CatConsumptionEnum consumption, int minMax) {
		String propString = null;
		
		if(prop == null) {
			prop = new Properties();
			try {
				prop.load(new FileInputStream(propertiesPath));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		if(consumption == CatConsumptionEnum.LOW) {
			if(minMax == 0)
				propString = "minConsumoBasso";
			else if(minMax == 1)
				propString = "maxConsumoBasso";
		}
		else if(consumption == CatConsumptionEnum.MEDIUM) {
			if(minMax == 0)
				propString = "minConsumoMedio";
			else if(minMax == 1)
				propString = "maxConsumoMedio";
		}
		else if(consumption == CatConsumptionEnum.HIGH) {
			if(minMax == 0)
				propString = "minConsumoAlto";
			else if(minMax == 1)
				propString = "maxConsumoAlto";
		}
		
		if(propString != null)
			return Double.valueOf(prop.getProperty(propString));
		else
			return -1;
	}
}
